# -*- coding: utf-8 -*-
# ----------------------------------------------------------------------------
# pyojo.examples                                  Copyright (c) 2013 nabla.net
# ----------------------------------------------------------------------------
""" Route map examples.
"""
import os
import pyojo



@pyojo.route("/")
def HomePage(request):
    links = [("/request","See pyojo Request attributes"),
             ("/environ","See HTTP request environ"),
             ("/environ_all","See complete environ"),
             ("/routes","See configured routes"),
             ("/noargs","Handler can have no arguments."),
             ("/reply","Reply subclass route example."),
             ("/status/200","Status OK"),
             ("/status/403","Forbidden"),
             ("/status/404","Not Found"),
             ]
    
    request.content_type("text/html")
    html = "<html><head><title>pyojo route Examples</title></head><body>"
    html +="<p>Examples:</p><ul>"
    for link, text in links:
        html += "<li><a href='%s'>%s</a></li>" % (link, text)
    html +="</ul></body></html>"
    return html


@pyojo.route("/request")
def _request_example(request):
    out = {"url": request.url,
           "method": request.method,
           "accept": request.accept,
           "id": id(request),
           "id os.environ": id(os.environ),
           "id environ": id(request.environ)}
    return pyojo.pretty(out)


@pyojo.route("/environ")
def _environ_example(environ):
    return pyojo.pretty(pyojo.http_request_headers(environ))


@pyojo.route("/environ_all")
def _environ_all_example(environ):
    return pyojo.pretty(environ)


@pyojo.route("/routes")
def _config_example():
    routes = pyojo.Config.get_route_map()
    return pyojo.pretty(routes)


@pyojo.route("/noargs")
def _no_args_example():
    return ["A function without arguments."]


@pyojo.route("/status/{code}")
def _status_codes(code):
    return int(code)


@pyojo.route("/reply")
class _ReplyExample(pyojo.Reply):
    
    def GET(self):
        self.content = "text/plain"
        self.write("%s.GET method called\n" % self.__class__.__name__)
        self.write("request = '%s'\n" % self.request)
        self.write("content = '%s'\n" % self.content)
        self.write("id()=%s\n" % (id(self)))
        return self


@pyojo.route("/{.*}")
def AllRoutes(request):
    return ["Route '%s'" % request.url]

# ----------------------------------------------------------------------------

if __name__ == '__main__':
    from pyojo.server import main
    main()

# ----------------------------------------------------------------------------

